package com.nativetalk.service.member.course.search;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.course.TdNtCoursePictureList;
import com.nativetalk.bean.course.TsNtCourseInfoDetail;
import com.nativetalk.bean.course.catalog.TdNtCourseCatalog;
import com.nativetalk.bean.member.course.TdNtMembCourseDetail;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.TdNtMembCourseDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 16/3/8.
 */
@Service
public class MembCourseSearchServiceImpl implements MembCourseSearchService {
    private static Logger logger = Logger.getLogger(MembCourseSearchServiceImpl.class);
    @Resource
    private TdNtMembCourseDao tdNtMembCourseDao;

    @Override
    public TdNtMembCourseDetail findTdNtMembCourseDetail(int memb_id, int cou_id) {
        return tdNtMembCourseDao.findTdNtMembCourseDetail(memb_id, cou_id);
    }

    @Override
    public TdNtMembCourseDetail findTdNtMembCourseDetailByMembId(int memb_id) {
        return tdNtMembCourseDao.findTdNtMembCourseDetailByMembId(memb_id);
    }

    @Override
    public RetInfo findTdNtMembCourseDetailByMembId(Page page) {
        String logInfo = this.getClass().getName() + ":findTdNtMembCourseDetailByMembId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtMembCourseDao.findTdNtMembCourseDetailByMembIdSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord / pageSize;
                } else {
                    totalPage = (totalRecord / pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage() - 1) * pageSize);
                List<TdNtMembCourseDetail> tdNtMembCourseDetails = tdNtMembCourseDao.findTdNtMembCourseDetailByMembIdPage(page);
                List<Map<String, Object>> list = new ArrayList<>();
                Map<String, TsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String, TsNtCourseInfoDetail>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);

                for (int i = 0; i < tdNtMembCourseDetails.size(); i++) {
                    Map<String, Object> map = new HashMap<>();
                    TdNtMembCourseDetail tdNtMembCourseDetail = tdNtMembCourseDetails.get(i);
                    TsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(tdNtMembCourseDetail.getCou_id() + "");
                    map.put("cou_name", tsNtCourseInfoDetail.getCou_name());
                    Map<String, List<TdNtCoursePictureList>> tdNtCourseBannerListMap = (Map<String, List<TdNtCoursePictureList>>) MemcachedUtils.get(MemcachedKey.tdNtCourseBannerListMap);
                    List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCourseBannerListMap.get(tdNtMembCourseDetail.getCou_id() + "");
                    map.put("picture", tdNtCoursePictureLists.get(0).getImg_url());
                    Map<String, List<TdNtCourseCatalog>> tdNtCourseCatalogMap = (Map<String, List<TdNtCourseCatalog>>) MemcachedUtils.get(MemcachedKey.tdNtCourseCatalogMap);
                    List<TdNtCourseCatalog> tdNtCourseCatalogs = tdNtCourseCatalogMap.get(tdNtMembCourseDetail.getCou_id() + "");
                    int size = 0;

                    for (int j = 0; j < tdNtCourseCatalogs.size(); j++) {
                        if (j < (tdNtCourseCatalogs.size() - 1)) {
                            if (tdNtMembCourseDetail.getStudy_page() >= tdNtCourseCatalogs.get(j).getEnd_page() && tdNtMembCourseDetail.getStudy_page() >= tdNtCourseCatalogs.get(j + 1).getEnd_page()) {
                                size = j + 1;
                                break;
                            }
                        } else {
                            if (tdNtMembCourseDetail.getStudy_page() >= tdNtCourseCatalogs.get(j).getEnd_page()) {
                                size = j + 1;
                                break;
                            }
                        }
                    }
                    map.put("size", size);
                    map.put("cou_id", tdNtMembCourseDetail.getCou_id());
                    map.put("cate_id", tsNtCourseInfoDetail.getCate_id());
                    map.put("totalSize", tdNtCourseCatalogs.size());
                    list.add(i, map);
                }
                Map<String, Object> result = new HashMap<>();
                result.put("list", list);
                result.put("totalPage", totalPage);
                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取我的课程成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("你还没开始学习");
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    /**
     * 查询该学生是否购买过着没课的体验套餐
     *
     * @param memb_id
     * @param cou_id
     * @return
     */
    public void findTdNtMembTrailSetmeal(Map map,Integer memb_id, Integer cou_id) {
        Integer is_trial = 0;// 0未购买体验套餐；1购买过
        Integer trialSetmealCount = tdNtMembCourseDao.findBuySetmealOfCourse(memb_id, cou_id);
        if (trialSetmealCount != null && trialSetmealCount > 0) {
            is_trial = 1;
        }
        Integer is_buy = 0; //0：未购买该课程    n:购买该课程数量
        is_buy = tdNtMembCourseDao.findMembIsBuy(memb_id,cou_id);
        map.put("isTrial", is_trial);
        map.put("is_buy",is_buy);
    }

    /**
     * 根据分类id查询该学生是否购买了该类别下的体验课
     *
     * @param memb_id
     * @param cateId
     * @return
     */
    public Integer findTdNtMembTrailSetmealWithCateId(Integer memb_id, String cateId) {
        Integer is_trial = 0;// 0未购买体验套餐；1购买过
        Integer trialSetmealCount = tdNtMembCourseDao.findBuySetmealOfCourseWithCateId(memb_id, Integer.valueOf(cateId));
        if (trialSetmealCount != null && trialSetmealCount > 0) {
            is_trial = 1;
        }
        return is_trial;
    }
}
